const Koa = require('koa');

const bodyParser = require('koa-bodyparser');

const controller = require('./controller');

const rest = require('./rest');

const path = require('path');

const cors = require('koa-cors');

const koaBody = require('koa-body');

const serve = require('koa-static');

const jwt = require('jsonwebtoken');

const jwtKoa = require('koa-jwt');

const secret = require('./secret').secret;

const app = new Koa();

// log request URL:
app.use(async (ctx, next) => {
    console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
    await next();
});

app.use(koaBody({ multipart: true }));

app.use(serve(path.join(__dirname, '/download')));

app.use(cors());

// parse request body:
app.use(bodyParser());

// bind .rest() for ctx:
app.use(rest.restify());

// add controllers:
app.use(controller());

app.use(jwtKoa({secret}).unless({
    path: [/^\/api\/login/, /^\/api\/regin/, /^\/api\/downloadPhoto/]//数组中的路径不需要通过jwt验证
}));

app.listen(3000);
console.log('app started at port 3000...');
